package impl

import (
	"context"
	"gitee.com/kevin186/keyauth/apps/menu"

	"github.com/infraboard/mcube/exception"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

// GET, Describe
// filter 过滤器(Collection),类似于MYSQL Where条件
// 调用Decode方法来进行 反序列化  bytes ---> Object (通过BSON Tag)
func (s *service) get(ctx context.Context, req *menu.DescribeRoleRequest) (*menu.MenuRole, error) {
	filter := bson.M{}

	filter["_id"] = req.Id
	filter["username"] = req.Name

	ins := menu.NewNillMenuRole()

	if err := s.col.FindOne(ctx, filter).Decode(ins); err != nil {
		if err == mongo.ErrNoDocuments {
			return nil, exception.NewNotFound("user %s not found", req)
		}

		return nil, exception.NewInternalServerError("find user %s error, %s", req, err)
	}

	return ins, nil
}
